package com.runner;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.runner.device.Device;
import com.runner.device.DeviceAndroid;
import com.runner.device.DeviceIOS;
import com.runner.device.DevicePC;
import com.runner.observer.Subject;
import com.runner.screen.GameScreen;
import com.runner.screen.LoadingAssetsScreen;
import com.runner.screen.MenuScreen;
import com.runner.services.GameServiceInterface;
import com.runner.services.GoogleServices;
import com.runner.sound.AudioManager;
import com.runner.sound.ConsoleAudio;
import com.runner.utility.Constants;
import com.runner.utility.Locator;

public class RedRunner extends Game implements Constants {
	private MenuScreen menu;
	private GameScreen gameScreen;
	private AudioManager audioManager;
	private OrthographicCamera camera;
	private Vector2 gravityVec;
	private float gravity;
	private World world;
	private Subject subject;
	private GameServiceInterface leaderboard;
	private GoogleServices services;
	private FitViewport viewport;
	
	public static float GRAVITY=3.5f * RELATIVE_SPEED * 1.55f;

	public RedRunner() {
		services = new GoogleServices();
	}

	public RedRunner(GameServiceInterface leaderboard, GoogleServices services) {
		this.leaderboard = leaderboard;
		this.services = services;
	}

	@Override
	public void create() {
		Device device;
		switch (Gdx.app.getType()) {
		case Android:
			device = new DeviceAndroid(leaderboard);
			break;
		case Desktop:
			device = new DevicePC();
			break;
		case iOS:
			device = new DeviceIOS(leaderboard);
			break;
		default:
			device = new DevicePC();
			break;

		}
		camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
		camera.position.set(VIRTUAL_WIDTH / 2, VIRTUAL_HEIGHT / 2, 0);
		camera.zoom = 1.0f;
		camera.update();
		
		viewport = new FitViewport(ACTUAL_VIRTUAL_WIDTH,ACTUAL_VIRTUAL_HEIGHT);
		viewport.update();
		
		Locator.init(this, device, services, new ConsoleAudio(), camera, viewport);
		
		subject = new Subject();

		gravityVec = new Vector2(0, 0);
		world = new World(gravityVec, false);
		Locator.setWorld(world);

		setScreen(new LoadingAssetsScreen());
	}

	public void init() {
		audioManager = new AudioManager(Locator.getAudio(), Locator.isMute(), subject);
		gameScreen = new GameScreen(camera, audioManager, subject, this);
		menu = new MenuScreen(this, audioManager, subject, camera);

		setGravity(-GRAVITY*Locator.getYGameSpeed());
		
		setScreen(menu);
	}

	public void setGravity(float g) {
		gravity = g;
		gravityVec.set(0, gravity);
		world.setGravity(gravityVec);
	}

	public GameScreen getGameScreen() {
		return gameScreen;
	}

	public MenuScreen getMenuScreen() {
		return menu;
	}
	
	public void adClosed(){
		gameScreen.resumeFromAd();
	}

	@Override
	public void render() {
		super.render();
	}

	@Override
	public void dispose() {
		super.dispose();
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
	}

	@Override
	public void pause() {
		super.pause();
	}

	@Override
	public void resume() {
		super.resume();
	}
}
